package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.IndicatorSystemDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IndicatorMapper;
import com.ruoyi.system.service.IIndicatorService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-14
 */
@Service
public class IndicatorServiceImpl implements IIndicatorService 
{
    @Autowired
    private IndicatorMapper indicatorMapper;

    @Autowired
    private IndicatorSystemDataMapper indicatorSystemDataMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Indicator selectIndicatorById(Long id)
    {
        return indicatorMapper.selectIndicatorById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param indicator 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Indicator> selectIndicatorList(Indicator indicator)
    {
        return indicatorMapper.selectIndicatorList(indicator);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param indicator 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIndicator(Indicator indicator)
    {
        indicator.setCreateTime(DateUtils.getNowDate());
        return indicatorMapper.insertIndicator(indicator);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param indicator 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIndicator(Indicator indicator)
    {
        if(indicatorSystemDataMapper.selectIndicatorSystemDataCountById(indicator.getId())>0)
            return -1;
        return indicatorMapper.updateIndicator(indicator);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorByIds(Long[] ids)
    {
        //存在指标和体系关联或还有子指标时无法删除
        for(Long id :ids)
            if(indicatorSystemDataMapper.selectIndicatorSystemDataCountById(id)>0||indicatorMapper.selectChildCountById(id)>0)
                return -1;

        return indicatorMapper.deleteIndicatorByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorById(Long id)
    {
        return indicatorMapper.deleteIndicatorById(id);
    }

    @Override
    public int selectChildCountById(Long id) {
        return indicatorMapper.selectChildCountById(id);
    }


    public List<Indicator> getIndicatorsBySystemId(Long systemId) {
        System.out.println("HQY_DEBUG"+systemId);
        List<IndicatorSystemData> IndicatorSystemDataList
                = indicatorSystemDataMapper.selectIndicatorSystemDataBySystemId(systemId);
        //List<Indicator> indicatorList = new ArrayList<>();
        Map<Long,Indicator> indicatorMap = new HashMap<>();
        for(IndicatorSystemData indicatorSystemData1 : IndicatorSystemDataList){
            //indicatorList.add(indicatorMapper.selectIndicatorById(indicatorSystemData1.getIndicatorId()));
            Indicator i = indicatorMapper.
                    selectIndicatorById(indicatorSystemData1.getIndicatorId());
            i.setSubjectiveWeight(indicatorSystemData1.getSubjectiveWeight());
            indicatorMap.put(indicatorSystemData1.getIndicatorId(),i);
        };
        List<Indicator> result = new ArrayList<>();
        for(Indicator indicator : indicatorMap.values()){
            if(indicator.getParentIndicatorId() == null){
                result.add(indicator);
                continue;
            }
            indicatorMap.get(indicator.getParentIndicatorId()).addChildren(indicator);
        }
        return result;
    }

    @Override
    public List<Indicator> getIndicatorByParentId(Long parentId) {
        return null;
    }

    @Override
    public List<Indicator> getIndicatorByChildrenId(List<Long> parentIds) {
        return null;
    }

    @Override
    public List<Indicator> getIndicatorListInTree() {
        List<Indicator> indicatorList = indicatorMapper.selectIndicatorList(new Indicator());
        Map<Long,Indicator> indicatorMap = new HashMap<>();
        for(Indicator indicator: indicatorList){
            indicatorMap.put(indicator.getId(), indicator);
        }
        List<Indicator> result = new ArrayList<>();
        for(Indicator indicator : indicatorMap.values()){
            if(indicator.getParentIndicatorId() == null){
                result.add(indicator);
                continue;
            }
            indicatorMap.get(indicator.getParentIndicatorId()).addChildren(indicator);
        }
        return result;
    }

    @Override
    public List<Indicator> getBasicIndicatorsBySystemId(Long id, int level) {
        List<IndicatorSystemData> list = indicatorSystemDataMapper.selectIndicatorSystemDataBySystemId(id);
        List<Indicator> res = new ArrayList<>();
        for(IndicatorSystemData indicatorSystemData : list){
            Indicator indicator = indicatorMapper.selectIndicatorById(indicatorSystemData.getIndicatorId());
            if(indicator.getIndicatorLevel() == level){
                res.add(indicator);
            }
        }
        return res;
    }

    @Override
    public List<IndicatorSystem> search(QueryVo queryVo) {
        return indicatorMapper.selectIndicatorListUseQueryParam(queryVo);
    }

    @Override
    public IndicatorVo getIndicatorVoBySystemIdAndIndicatorId(Long systemId, Long indicatorId) {
        Indicator indicator = indicatorMapper.selectIndicatorById(indicatorId);

        IndicatorSystemData indicatorSystemData = new IndicatorSystemData();
        indicatorSystemData.setIndicatorId(indicatorId);
        indicatorSystemData.setSystemId(systemId);
        List<IndicatorSystemData> indicatorSystemDataList = indicatorSystemDataMapper.selectIndicatorSystemDataBySystemIdAndIndicatorId(indicatorSystemData);
        if(indicatorSystemDataList.size() == 0){
            return IndicatorVo.builder()
                    .calculationMethod(indicator.getCalculationMethod())
                    .dataSource(indicator.getDataSource())
                    .description(indicator.getDescription())
                    .id(indicator.getId())
                    .indicatorDescription(indicator.getIndicatorDescription())
                    .indicatorLevel(indicator.getIndicatorLevel())
                    .indicatorName(indicator.getIndicatorName())
                    .parentIndicatorId(indicator.getParentIndicatorId()).build();
        }
        return IndicatorVo.builder()
                .calculationMethod(indicator.getCalculationMethod())
                .dataSource(indicator.getDataSource())
                .description(indicator.getDescription())
                .id(indicator.getId())
                .indicatorDescription(indicator.getIndicatorDescription())
                .indicatorLevel(indicator.getIndicatorLevel())
                .indicatorName(indicator.getIndicatorName())
                .parentIndicatorId(indicator.getParentIndicatorId())
                .subjectiveWeight(indicatorSystemDataList.get(0).getSubjectiveWeight()).build();
    }

    /**
     * 返回 Map<indicatorName,desc>
     */
    @Override
    public List<Map<String,String>> getIndicatorName_DescMap(){
        return indicatorMapper.getIndicatorName_DescMap();
    }
}
